package com.xxx.client.android.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Gallery;

import com.xxx.client.android.CustomApplication;
import com.xxx.client.android.R;
import com.xxx.client.android.adapter.SceneLargeAdapter;
import com.xxx.client.android.api.util.Ksoap2Caller;
import com.xxx.client.android.util.Tool;

public class SceneActivity extends Activity {

	private Gallery mGallery;

	private ArrayList<String> sceneFileNameList;
	private int position;

	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.scene);
		mGallery = (Gallery) findViewById(R.id.Gallery);

		if (savedInstanceState == null) {
			sceneFileNameList = (ArrayList<String>) getIntent().getExtras()
					.getSerializable("sceneFileNameList");
			position = getIntent().getExtras().getInt("position");
		} else {
			sceneFileNameList = (ArrayList<String>) savedInstanceState
					.getSerializable("sceneFileNameList");
			position = savedInstanceState.getInt("position");
		}

		SceneLargeAdapter imageNewsListAdapter = new SceneLargeAdapter(
				SceneActivity.this);
		imageNewsListAdapter.setList(sceneFileNameList);
		mGallery.setAdapter(imageNewsListAdapter);
		mGallery.setSelection(position, true); // animate
												// to
												// center
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		sceneFileNameList = (ArrayList<String>) savedInstanceState
				.getSerializable("sceneFileNameList");
		position = savedInstanceState.getInt("position");
		if (sceneFileNameList != null) {
			SceneLargeAdapter imageNewsListAdapter = new SceneLargeAdapter(
					SceneActivity.this);
			imageNewsListAdapter.setList(sceneFileNameList);
			mGallery.setAdapter(imageNewsListAdapter);
			mGallery.setSelection(position, true); // animate to center
		}
		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		if (sceneFileNameList != null) {
			outState.putSerializable("sceneFileNameList", sceneFileNameList);
			outState.putInt("position", position);
		}
		super.onSaveInstanceState(outState);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.about_menu_item:
			Tool.about(this);
			break;
		case R.id.refresh_menu_item:
			refresh();
			break;
		default:
		}
		return super.onOptionsItemSelected(item);
	}

	public void refresh() {
		Ksoap2Caller.clearCache();
		CustomApplication.getInstance().getImageCache().clearImageCache();
		SceneLargeAdapter imageNewsListAdapter = new SceneLargeAdapter(
				SceneActivity.this);
		imageNewsListAdapter.setList(sceneFileNameList);
		mGallery.setAdapter(imageNewsListAdapter);
		mGallery.setSelection(position, true); // animate to center
	}

}
